package com.itheima.management.service;


import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.management.mapper.UserInfoMapper;
import com.itheima.management.pojo.UserInfo;
import com.itheima.management.vo.PageResult;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.itheima.management.vo.ManageComment;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ManageCommentService {

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    public PageResult queryCommentList(String publishId, Integer page, Integer pageSize, String sortProp, String sortOrder) {

        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);

        PageInfo<Comment> pageInfo = this.quanZiApi.queryCommentList(publishId, page, pageSize, sortProp, sortOrder);
        List<Comment> records = pageInfo.getRecords();


        List<ManageComment> CommentsVoList = new ArrayList<>();
        //
        for (Comment record : records) {

            ManageComment CommentsVo = new ManageComment();
            CommentsVo.setId(record.getId().toHexString());
            QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", record.getUserId());
            UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
            CommentsVo.setNickname(userInfo.getNickName());
            CommentsVo.setContent(record.getContent());
            CommentsVo.setUserId(record.getUserId().intValue());
            CommentsVo.setCreateDate(record.getCreated());

            CommentsVoList.add(CommentsVo);
        }
        pageResult.setItems(CommentsVoList);
        return pageResult;
    }
}
